package com.atguigu.gulimall.cart.controller;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import com.atguigu.gulimall.cart.vo.UserInfoTo;
import io.lettuce.core.output.NestedMultiOutput;
import org.apache.coyote.http11.upgrade.UpgradeServletOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.function.SupplierUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;
@Controller
public class CartController {
    @Autowired
    private CartService cartService;
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> currentUserCartItems(){  //  获取单个用户的购物车添加的商品信息
//        return  cartService.getUserCartItems();
        List<CartItem> userCartItems = cartService.getUserCartItems();

        return  userCartItems;


    }
@GetMapping("/deleteItem")
public String deleteItem(@RequestParam Long skuId){//  删除单个购物项
    cartService.deleteItem(skuId);
    return  "redirect:http://cart.gulimall.com/cart.html";
}
    @GetMapping("/countItem")
    public String countItem(@RequestParam  Long  skuId,@RequestParam Integer num){
        cartService.changeItemCount(skuId,num);
        return  "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam  Long  skuId,@RequestParam Integer check){
        cartService.checkItem(skuId,check);
        return  "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/cart.html")
    public String  cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart =  cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }
    /*
    把sku 添加到 购物车的 方法
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam Long skuId , @RequestParam Integer num, RedirectAttributes ra) throws ExecutionException, InterruptedException {
 cartService.addToCart(skuId,num);//   调用service方法
// model.addAttribute("skuId",skuId);
    ra.addAttribute("skuId",skuId);//然后把sku的id放进作用域
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }
/*
跳转到   添加购物车  的  成功页
 */
    @GetMapping("/addToCartSuccess.html")
    public String   addToCartSuccessPage(@RequestParam Long skuId,Model model){
        //重定向到成功页面,再次查询购物车数据
        CartItem  item=cartService.getCartItem(skuId);
        System.out.println("item = " + item);
        model.addAttribute("item",item);
        return "success";
    }
}
